<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link type="text/css" rel="stylesheet" href="css/active4d.css">
        <link type="text/css" rel="stylesheet" href="css/main.css">
        <title>A detailed description of the Joo language syntax</title>
    </head>
    <body>
        <h1>A detailed description of the Joo language syntax <a class='hh' name='A_detailed_description_of_the_Joo_language_syntax'>⁋</a></h1>
 <i>Working draft.</i>
 <i>To learn basics of JooScript, take a look at <a href="http://eloquentjavascript.net/">Eloquent JavaScript</a>, good <a href="JavaScript.html">JavaScript</a> tutorial.</i>
<h2>Introduction <a class='hh' name='Introduction'>⁋</a></h2><p>Joo was designed in mind to be simple for regular (not-senior-programmer) users, but powerful enough to make complex web-apps. <h3>Sample: module.joo <a class='hh' name='Sample:_module.joo'>⁋</a></h3></p><pre class="active4d code-block">
  doc XML <span class="Operator">=</span> <span class="Operator">&lt;</span>doc id<span class="Operator">=</span><span class="String"><span class="String">&quot;</span>node1234<span class="String">&quot;</span></span>/<span class="Operator">&gt;</span>  <span class="LineComment"><span class="LineComment">//</span> module variable is XML fragment</span>
  
  <span class="Keyword">package</span> a.b.c {               <span class="LineComment"><span class="LineComment">//</span> operate in a.b.c namespace </span>
    <span class="Storage">public</span> <span class="Storage">class</span> D (){          <span class="LineComment"><span class="LineComment">//</span> class is a member of group a.b.c</span>
      id <span class="LibraryClassType">String</span>                 <span class="LineComment"><span class="LineComment">//</span> public variable definition</span>
      <span class="Storage">function</span> <span class="FunctionName">new</span> () {         <span class="LineComment"><span class="LineComment">//</span> class constructor</span>
        self.<span class="NamedConstant">id</span> <span class="Operator">=</span> getNode().<span class="NamedConstant">id</span>
      }
      <span class="Storage">function</span> <span class="FunctionName">getNode</span> () {     <span class="LineComment"><span class="LineComment">//</span> public class method</span>
        let node <span class="Operator">=</span> {            <span class="LineComment"><span class="LineComment">//</span> local variable</span>
          frozen id: doc.@id    <span class="LineComment"><span class="LineComment">//</span> object property is attribute value</span>
        }
        <span class="Keyword">return</span> node
      }
    }
  }
</pre>
<h2>Namespaces <a class='hh' name='Namespaces'>⁋</a></h2><p>Variables, object properties, functions or classes could be declared with namespace declaration.  <h3>Pseudo-namespaces <a class='hh' name='Pseudo-namespaces'>⁋</a></h3></p><p><strong>final</strong> <code>[class, function, object]</code>:  <ul><li>shorthand for <a href="EcmaScript.html">EcmaScript</a> 5's <code>Object.preventExtensions( obj )</code></li></ul></p><p><strong>sealed</strong> <code>[class, function, object, object property, function member, class member]</code>:  <ul><li>set flag <code>configurable</code> for object property(es) to false</li><li>=> <code>Object.seal( obj )</code></li><li>=> <code>Object.defineProperty( obj, "prop", { configurable: false })</code></li></ul></p><p><strong>frozen</strong> <code>[class, function, object, object property, function member, class member]</code>:  <ul><li>set flag <code>writable</code> for object property(es) to false</li><li>=> <code>Object.freeze( obj )</code></li><li>=> <code>Object.defineProperty( obj, "prop", { writable: false })</code></li></ul></p><p><strong>hidden</strong> <code>[class, function, object, object property, function member, class member]</code>:  <ul><li>set flag <code>enumerable</code> for object property(es) to false</li><li>=> <code>Object.wash( obj )</code>, <i>non-standard method</i>, force <code>enumerable</code> flag of all <code>obj</code> properties to false</li><li>=> <code>Object.defineProperty( obj, "prop", { enumerable: false })</code></li></ul></p><p><strong>internal</strong> <code>[class, function, object, object property, function member, class member]</code>:  <ul><li>shorthand for <code>sealed frozen hidden</code></li></ul></p><p><strong>public</strong> <code>[module member, group member, function member, class member]</code>:  <ul><li>visible for outer world</li></ul></p><p><strong>static</strong> <code>[function member, class member]</code>:  <ul><li>binding to context object where it declared</li></ul></p><p><strong>global</strong> <code>[var]</code>:  <ul><li>global namespace</li><li>in browser, <code>global</code> object is <code>window</code>, in flash - <code>globals</code></li><li>shorthand: <code>$var</code></li></ul></p><p><strong>local</strong> <code>[var]</code>:  <ul><li>create variable in <a href="JooLanguageSyntax.html#Variable_scope">local scope</a></li></ul></p><p><strong>macro</strong> <code>[var]</code>:  <ul><li><a href="JooLanguageSyntax.html#Macro_expressions"> macro variable</a></li><li>shorthand: <code>%var</code></li></ul></p><p><strong>get</strong> <code>[object property, function member, class member]</code>:  <ul><li>getter</li></ul></p><p><strong>set</strong> <code>[object property, function member, class member]</code>:  <ul><li>setter</li></ul></p><p><strong>Note</strong>: Double colon (<code>::</code>) using for direct access to the namespace object. E.g. <code>public::x = 1</code>, <code>local::y = .123</code> etc... <h3>Defining namespaces <a class='hh' name='Defining_namespaces'>⁋</a></h3></p><p>To set up your own namespace, use this syntax: </p><pre class="active4d code-block">
  namespace name [<span class="Operator">=</span> uri]
</pre><p>Alternative variant:</p><pre class="active4d code-block">
  let myNS Namespace <span class="Operator">=</span> Namespace(uri)
</pre><p>Example:</p><pre class="active4d code-block">
  namespace google <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>http://google.com/ns/v10/<span class="String">&quot;</span></span>
  google name <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>Google<span class="String">&quot;</span></span>
 
  namespace yahoo;
  yahoo name <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>Yahoo<span class="String">&quot;</span></span>
 
  google <span class="Storage">function</span> <span class="FunctionName">getName</span>() <span class="Variable">this</span>.<span class="NamedConstant">name</span>;
  yahoo <span class="Storage">function</span> <span class="FunctionName">getName</span>() <span class="Variable">this</span>.<span class="NamedConstant">name</span>;
 
  <span class="CommandMethod">print</span>(google::getName(), yahoo::getName(), getName()) <span class="LineComment"><span class="LineComment">//</span> =&gt; Google Yahoo Yahoo</span>
</pre><p>Namespace can be defined only once and will be visible only in current context.</p><p><!-- Здесь снова неопределённый момент: как быть с пространство имён функции? Если это public , то к какому объекту они относятся? К функции или к глобальному? В питоне переменная находится в пространстве имен функции. То есть как бы local... --></p><p><!-- <strong>Note</strong>: in <a href="JooScript.html">JooScript</a> default namespace for undeclared variable in modules, functions and classes is <code>public</code> (pythonic style), except for packages, whose namespace is <code>local</code>.  For <a href="JavaScript.html">JavaScript</a> variables, default namespace is undefined and that may cause parser show warning or error if value assigned to undeclared variable in strict mode. --></p><p>To change default namespace: </p><pre class="active4d code-block">
  <span class="Keyword">default</span> namespace <span class="Operator">=</span> myNS
</pre>
<h2>Variable scope <a class='hh' name='Variable_scope'>⁋</a></h2><p>In <a href="JooScript.html">JooScript</a> <code>var</code> is deprecated. Instead you should use <code>let</code> for defining temporary variables. In contrast to <code>var</code>, <code>let</code> defines variables whose scope is constrained to the block in which they're defined. This syntax is very much like the syntax used for <code>var</code>.</p><p>Example:</p><pre class="active4d code-block">
  let gamma <span class="Operator">=</span> <span class="Number">1</span>
  <span class="Keyword">if</span> (gamma <span class="Operator">&gt;</span> <span class="Number">0</span>) {
    let gamma <span class="Operator">=</span> <span class="Number">4</span>
  }
  <span class="CommandMethod">print</span>(gamma) <span class="LineComment"><span class="LineComment">//</span> will output 1</span>
</pre><p>More info about <a href="BlockScopeWithLet.html">block scope with let</a>. <h2>Roles and Types <a class='hh' name='Roles_and_Types'>⁋</a></h2></p><p><a href="http://en.wikipedia.org/wiki/Role_%28computer_science%29">Role-oriented programming</a> is a form of computer programming aimed at expressing things in terms which are analogous to our conceptual understanding of the world. This should make programs easier to understand and maintain.</p><p>The main idea of role-oriented programming is that humans think in terms of roles. This claim is often backed up by examples of social relations. For example, a student attending a class and the same student at a party are the same person, but yet plays in two different roles. In particular, the interactions of this person with the outside world depends on his current role. The roles typically share features, e.g., the intrinsic properties of being a person. This sharing of properties is often handled by the delegation mechanism.</p><p><!-- Также, как и в жизни, роли переменной-персоны могут меняться с течением "жизни программы". Она может строкой, затем числом, затем снова строкой и в конце становится "пустым" значением. Использование определение ролей для переменной или свойства класса не обязательно, но необходимо, если вы не хотите запутаться в вашей пьесе-программе. Это также позволит содержать ваш код в читабельном состоянии, а также предоставит возможность другим лучше понять вашу логику. --></p><p>As well as in real live, roles of a variable-person may changes during "life of a program". It may be string, then number, then again string and in the end became void. Using roles or types for variable is not necessary but you have to using it if you do not want to get confused by your script. It also allow keep your code readable, as a result, others can better understand your logic.</p><p>Another positive side is that when are used roles or types you are always in sure that result of the program is what you waiting for. Also it is handy thing for debugging.  <h3>Basic types <a class='hh' name='Basic_types'>⁋</a></h3></p><p><!-- Есть несколько основных базовых типов, которые вы можете использовать в своей программе. Например, строка или целое число.</p><p>Из базовых типов также можно констрировать новые роли. Однако, базовые типы не могут быть использованы в качестве родительких ролей. --></p><p>There are a few basic types, which you can use in your program. For example, string or number. Types cannot be neither extended nor created inside scripts.</p><p>It is possible to construct new roles from basic types. However, basic types cannot be used as inherited roles. That is <code>role</code> and <code>type</code> have different meanings.</p><p>You may think of roles as a mockup for object.</p><p>Here is a list of basic types:</p><p><!-- Int, Uint, Float должны также принимать второй аргумент как размерность числа, например Int(53, 8). --></p><p><!-- Вектор на самом деле не примитивный тип данных.. Так же, как и массив. --></p><p><table border=1><tr><td> <i>JS name</i>  </td><td>  <i>Joo name</i>  </td><td>  <i>sample value</i> </td><td>  <i>type validator</i> </td><td>  <i>Joo class</i> </td><td>  <i>JS class</i>  </td></tr> <tr><td> <strong>object</strong>   </td><td>  <strong>object</strong>    </td><td>  <code>{x: 1, y: 2}</code> </td><td>  Type.isPrimitive() </td><td>  Object </td><td>  Object </td></tr> <tr><td> <strong>number</strong>   </td><td>  <strong>real number</strong>  </td><td>  <code>123</code>, <code>0.123</code> </td><td>  Type.isNumber() </td><td>  Number </td><td>  Number </td></tr> <tr><td> -   </td><td>  <strong>integer number</strong>  </td><td>  <code>-123</code> </td><td>  Type.isInt() </td><td>  Int </td><td>  - </td></tr> <tr><td> -   </td><td>  <strong>unsigned integer</strong>  </td><td>  <code>123</code> </td><td>  Type.isUint() </td><td>  Uint </td><td>  - </td></tr> <tr><td> -   </td><td>  <strong>floating point number</strong>    </td><td>  <code>0.123</code> </td><td>  Type.isFloat() </td><td>  Float </td><td>  - </td></tr> <tr><td> <strong>string</strong>   </td><td>  <strong>string</strong>   </td><td>  <code>"abc"</code> </td><td>  Type.isString() </td><td>  String </td><td>  String </td></tr> <tr><td> <strong>boolean</strong>  </td><td>  <strong>boolean</strong>  </td><td>  <code>true</code>, <code>false</code> </td><td>  Type.isBool() </td><td>  Boolean </td><td>  Boolean </td></tr> <tr><td> <strong>function</strong> </td><td>  <strong><a href="JooLanguageSyntax.html#Functions">function</a></strong> </td><td>  <code>function A(){}</code> </td><td>  Type.isFunc() </td><td>  Function </td><td>  Function </td></tr> <tr><td> <strong>function</strong> </td><td>  <strong><a href="JooLanguageSyntax.html#Classes">class</a></strong>    </td><td>  <code>class B(){}</code>    </td><td>  Type.isClass() </td><td>  Class </td><td>  Function </td></tr> <tr><td> - </td><td>  <strong><a href="JooLanguageSyntax.html#Vector_array">vector array</a></strong> </td><td>  <code>VectorArray(3, Int, 8)</code> </td><td>  - </td><td>  VectorArray </td><td>  - </td></tr> <tr><td> -          </td><td>  <strong><a href="JooLanguageSyntax.html#E4X:_XML-juggling">xml</a></strong>            </td><td>  <code>&lt;doc id="doc123"/&gt;</code>, <code>&lt;&gt;&lt;/&gt;</code> </td><td>  Type.isXML() </td><td>  XML, XMLList </td><td>  - </td></tr> <tr><td> <strong>null</strong>     </td><td>  <strong>null</strong>           </td><td>  <code>null</code> </td><td>  Type.isVoid() </td><td>  Null  </td><td>  - </td></tr> <tr><td> <strong>undefined</strong></td><td>  <strong>void</strong>           </td><td>  <code>void</code>, <code>undefined</code> </td><td>  Type.isVoid() </td><td>  Void  </td><td>  - </td></tr> </table></p><p>Thus, next expressions look strange, but they are valid and equivalent:</p><pre class="active4d code-block">
  <span class="Storage">function</span> <span class="FunctionName">foo</span> () {}
  foo <span class="LibraryClassType">Function</span> <span class="Operator">=</span> <span class="LibraryClassType">Function</span> ()
 
  <span class="Storage">class</span> baz () {}
  baz Class <span class="Operator">=</span> Class ()
</pre><p>Actually, some types just a fake because of <a href="JavaScript.html">JavaScript</a> limits. It is reserved for possible future. However you can use it to make your code clear.</p><p>More about types you can find in <a href="Types.html">related section</a>.</p><p><!-- In <a href="JavaScript.html">JavaScript</a> float and integer numbers are both the Number.  In <a href="JooScript.html">JooScript</a> these types will be <i>emulated</i>. --> <h3>Defining new roles <a class='hh' name='Defining_new_roles'>⁋</a></h3></p><p>Developers could define new roles, using this form:</p><pre class="active4d code-block">
  role name {
    property [is] type_or_role [, property [is] type_or_role [, ...]]
  }
</pre><p>Example:</p><pre class="active4d code-block">
  role Point {
    x is Float,
    y is Float
  }
 
  role Pet {
    tail is Float, 
    character is <span class="LibraryClassType">String</span>,
    age is Int,
    name is <span class="LibraryClassType">String</span>
  }
</pre><p>Role properties can be inherited from one parent role:</p><p><!-- - Почему роль не может наследовать свойства от нескольких родителей? - Это упрощает синтаксис. Проследить всю цепочку от родителя до потомка гораздо проще, если наследование линейное. Нелинейное наследование будет у классов. Кроме того, можно использовать конструктор Role() для особых случаев. Иллюстрация: вы наследуете ваши свойства от двух родителей, но когда вы выбираете профессию или вашу роль в обществе, вы выбираете такую, которая максимально подходит к вашим возможностям. То есть, вы подгоняете ваши абстрактные возможности под конкретную роль. Роль - сама по себе, вы - сами по себе. И если взять, к примеру (грубо), профессию архитектора, то она наследуется от роли художника, к которой добавляются необходимость знания математики и сопромата. --></p><pre class="active4d code-block">
  role Rectangle inherits Point {
    width is Float,
    height is Float
  }
 
  role Dog inherits Pet {
    fearless is Float,
    bow_wow is <span class="LibraryClassType">Function</span>
  }
 
  role Cat inherits Pet {
    whiskers is Int,
    miaow is <span class="LibraryClassType">Function</span>
  }
</pre><p><!-- Здесь есть спорный момент... Получается, что у функции две роли: первая - метод, другуая - конструктор. Это усложняет синтаксис. Я просто выключу оператор new. Тогда не будет путаницы с классами и функциями. --></p><p>Variable can be defined with type or role:</p><pre class="active4d code-block">
  let pA Point <span class="Operator">=</span> {x: <span class="Operator">-</span><span class="Number">123.3</span>, y: <span class="Number">0.34</span>}
  let pB Point <span class="Operator">=</span> {x: .<span class="Number">63</span>, y: <span class="Number">45.3</span>}
</pre><p>To check type of a returning value, add type or a role after the function name, as in examples:</p><pre class="active4d code-block">
  <span class="Storage">function</span> len Float (p1 Point, p2 Point) {
    let x <span class="Operator">=</span> p2.<span class="NamedConstant">x</span> <span class="Operator">-</span> p1.<span class="NamedConstant">x</span>
    let y <span class="Operator">=</span> p2.<span class="NamedConstant">y</span> <span class="Operator">-</span> p1.<span class="NamedConstant">y</span>
    <span class="Keyword">return</span> <span class="LibraryClassType">Math</span>.<span class="CommandMethod">sqrt</span>(x<span class="Operator">*</span>x <span class="Operator">+</span> y<span class="Operator">*</span>y)
  }
 
  let side Float <span class="Operator">=</span> len(pA, pB)
  let shape Rectangle <span class="Operator">=</span> {x: pA.<span class="NamedConstant">x</span>, y: pA.<span class="NamedConstant">y</span>, width: side, height: side}
</pre><p>You may also declare default role (but not basic type!) for classes, using keyword <code>as</code>:</p><pre class="active4d code-block">
  <span class="Storage">class</span> Cleo as Cat(){
    tail <span class="Operator">=</span> <span class="Number">12</span>
    character <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>calm<span class="String">&quot;</span></span>
    age <span class="Operator">=</span> <span class="Number">6</span>
    name <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>Cleo<span class="String">&quot;</span></span> 
    whiskers <span class="Operator">=</span> <span class="Number">51</span>
    <span class="Storage">function</span> <span class="FunctionName">miaow</span>() {
      trace(<span class="String"><span class="String">&quot;</span>miaow!<span class="String">&quot;</span></span>)
    }
  }
  
  <span class="Storage">class</span> Baks(){
    tail <span class="Operator">=</span> <span class="Number">5</span>
    character <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>active<span class="String">&quot;</span></span>
    age <span class="Operator">=</span> <span class="Number">14</span>
    name <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>Baks<span class="String">&quot;</span></span>
    fearless <span class="Operator">=</span> <span class="Number">0.1</span>
    <span class="Storage">function</span> <span class="FunctionName">bow_wow</span>() {
      trace(<span class="String"><span class="String">&quot;</span>bow wow!<span class="String">&quot;</span></span>)
    }
  }
 
  Baks as Dog <span class="LineComment"><span class="LineComment">//</span> this is just another form</span>
 
  let my_dog Dog <span class="Operator">=</span> Baks() <span class="LineComment"><span class="LineComment">//</span> if my_dog has no Dog properties,</span>
  let my_cat Cat <span class="Operator">=</span> Cleo() <span class="LineComment"><span class="LineComment">//</span> or my_cat has no Cat properties, </span>
                              <span class="LineComment"><span class="LineComment">//</span> in strict mode compiler will raise error,</span>
                              <span class="LineComment"><span class="LineComment">//</span> if no strict mode - warning</span>
</pre><p>Role name is declared in program scope so that you cannot set a variable and role with the same name.</p><p><strong>Note</strong>: Although modern <a href="JavaScript.html">JavaScript</a> engines as fast as rockets, it may be necessary to disable runtime typing, to speed up a program. To do this, use key <code>--disable-runtime-typing</code> with compiler. <h2>Type object <a class='hh' name='Type_object'>⁋</a></h2></p><p>In <a href="JooScript.html">JooScript</a> there is special object <code>Type</code>. It has several methods to test types, some of these was listed in basic types table. There is also method <code>Type.is_a()</code> to test if a variable is instance of some class. For example:</p><pre class="active4d code-block">
  Type.is_a(<span class="String"><span class="String">&quot;</span>abc<span class="String">&quot;</span></span>, <span class="LibraryClassType">String</span>) <span class="LineComment"><span class="LineComment">//</span> =&gt; true</span>
  Type.is_a(<span class="Number">123</span>, <span class="LibraryClassType">Number</span>) <span class="LineComment"><span class="LineComment">//</span> =&gt; true</span>
  Type.is_a(<span class="Number">123</span>, Int) <span class="LineComment"><span class="LineComment">//</span> =&gt; false</span>
  Type.is_a(my_dog, Dog) <span class="LineComment"><span class="LineComment">//</span> =&gt; true</span>
  Type.is_a(my_cat, Dog) <span class="LineComment"><span class="LineComment">//</span> =&gt; false</span>
</pre>
<h2>Vector array <a class='hh' name='Vector_array'>⁋</a></h2><p>Vector array is just typed array. Vector features is fixed length and fixed elements type. Vector array allows fast manipulations with ordered uniform data such as vectors or canvas image data.</p><p>Examples:</p><pre class="active4d code-block">
  <span class="Storage">var</span> buffer <span class="Operator">=</span> [<span class="Number">1</span>, <span class="Number">2</span>, <span class="Number">3</span>], type <span class="Operator">=</span> Int, intSize <span class="Operator">=</span> <span class="Number">8</span>;
  let vecA <span class="Operator">=</span> VectorArray( buffer, type, intSize ) 
  let vecB <span class="Operator">=</span> VectorArray( <span class="Number">3</span>, Uint, <span class="Number">32</span> )
  vecB[<span class="Number">0</span>] <span class="Operator">=</span> <span class="Number">123</span>, vecB[<span class="Number">1</span>] <span class="Operator">=</span> <span class="Number">456</span>, vecB[<span class="Number">2</span>] <span class="Operator">=</span> <span class="Number">789</span>
</pre><p><strong>Note:</strong> at this moment typed arrays only supported in Gecko 2.0-based browsers (Firefox 4). In other, VectorArray just return the Array. <h2>Macro expressions <a class='hh' name='Macro_expressions'>⁋</a></h2></p><p>To define variables, whose values could be calculated at compilining stage, use <code>macro</code> namespace or prefix <code>%</code> for variables:</p><pre class="active4d code-block">
  macro someValue <span class="Operator">=</span> <span class="Number">1</span> <span class="Operator">+</span> <span class="Number">2</span> <span class="Operator">+</span> <span class="Number">3</span>
</pre><p>or</p><pre class="active4d code-block">
  <span class="Operator">%</span><span class="Storage">var</span> <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>abc<span class="String">&quot;</span></span> <span class="LineComment"><span class="LineComment">//</span> all variables beginning with % assume to be macro</span>
  let <span class="Operator">%</span><span class="Storage">var</span> <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>def<span class="String">&quot;</span></span> <span class="LineComment"><span class="LineComment">//</span> macro definitions may be used to generate code</span>
</pre><p>If macro expression can not be evaluated, compiler will show fatal error. <h2>Strict mode <a class='hh' name='Strict_mode'>⁋</a></h2></p><p>Strict mode is a new feature in <a href="EcmaScript.html">EcmaScript</a> 5 and <a href="JooScript.html">JooScript</a> that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions (generally providing the user with more information and a tapered-down coding experience).</p><p>To enable strict mode:</p><pre class="active4d code-block">
  <span class="Storage">function</span> <span class="FunctionName">getX</span> () {
    <span class="String"><span class="String">&quot;</span>use strict<span class="String">&quot;</span></span>; <span class="LineComment"><span class="LineComment">//</span> from now getX will operate in strict context</span>
    a <span class="Operator">=</span> <span class="Number">1</span> <span class="LineComment"><span class="LineComment">//</span> compiler will invoke error, because of 'a' namespace is undeclared</span>
    <span class="Keyword">return</span> a
  }
</pre><p>You can find futher info about strict mode in <a href="EcmaScript5StrictMode.html">EcmaScript5StrictMode</a> part.</p><p><strong>Note</strong>: In contrast to EcmaScript, <code>with</code> statement is always valid in JooScript. <h2><code>with</code> statement <a class='hh' name='<code>with</code>_statement'>⁋</a></h2></p><p>In JooScript, <code>with</code> is static statement. While evaluated, is not looking for variables in the scope, therefore is not affecting execution speed.</p><p>Examples:</p><pre class="active4d code-block">
  <span class="Operator">with</span>(obj) { <span class="LineComment"><span class="LineComment">//</span> first form</span>
    x <span class="Operator">=</span> <span class="Number">1</span>; <span class="LineComment"><span class="LineComment">//</span> if x is not in actual scope, with will bind x to obj</span>
  }
 
  <span class="Operator">with</span>(<span class="LibraryClassType">Math</span>) self.rand <span class="Operator">=</span> <span class="CommandMethod">round</span>(<span class="CommandMethod">random</span>() <span class="Operator">*</span> <span class="Number">100</span>); <span class="LineComment"><span class="LineComment">//</span> second form</span>
</pre><pre class="active4d code-block">
  <span class="LineComment"><span class="LineComment">//</span> jooscript</span>
  let o <span class="Operator">=</span> {x: <span class="Number">1</span>, y: <span class="Number">2</span>, z: <span class="Operator">-</span><span class="Number">3</span>}
  <span class="Operator">with</span>(o) {
    z <span class="Operator">=</span> x <span class="Operator">+</span> y
  }
  <span class="CommandMethod">print</span>(o.z, z) <span class="LineComment"><span class="LineComment">//</span> =&gt; -3, 3</span>
  let z <span class="Operator">=</span> <span class="Number">0</span>     <span class="LineComment"><span class="LineComment">//</span> that's right, z is in local scope</span>
</pre><p>When project compiled into JavaScript, <code>with</code> statement will be translated into static expression. </p><pre class="active4d code-block">
  <span class="LineComment"><span class="LineComment">//</span> javascript, translated code</span>
  <span class="Storage">var</span> o, z;
  o <span class="Operator">=</span> {x: <span class="Number">1</span>, y: <span class="Number">2</span>, z: <span class="Operator">-</span><span class="Number">3</span>}
  {
    z <span class="Operator">=</span> o.<span class="NamedConstant">x</span> <span class="Operator">+</span> o.<span class="NamedConstant">y</span>
  }
  <span class="CommandMethod">print</span>(o.z, z)
  z <span class="Operator">=</span> <span class="Number">0</span>
</pre>
<h2>Logical operators <a class='hh' name='Logical_operators'>⁋</a></h2><p>In JooScript, logical operators <code>&&</code>, <code>||</code>, <code>!</code> (bang) is deprecated. You must using <code>and</code>, <code>or</code>  and <code>not</code> respectively.</p><pre class="active4d code-block">
  let a <span class="Operator">=</span> <span class="Number">1</span>, b <span class="Operator">=</span> <span class="Number">2</span>;
  let c <span class="Operator">=</span> a and b ? a <span class="Operator">+</span> b : <span class="Number">0</span>;
  <span class="Keyword">if</span> ((isInt(a) or isFloat(a)) and not isString(b)) { <span class="BlockComment"><span class="BlockComment">/*</span> ... <span class="BlockComment">*/</span></span> }
</pre>
<h2>Functions <a class='hh' name='Functions'>⁋</a></h2><p>Function is the same as in JavaScript, except for function cannot be used as object constructors. That is operator <code>new</code> is deprecated, use <code>class</code> instead.</p><p>General syntax:</p><pre class="active4d code-block">
  [namespace] <span class="Storage">function</span> [funId] [type or role] (argA, argB [, ... rest]) {
    [namespace] member [<span class="Operator">=</span> expression [, ...]] 
    [<span class="Keyword">return</span> [expression]]
  }
</pre>
<h3>Lambda functions <a class='hh' name='Lambda_functions'>⁋</a></h3><p>Lambda expression is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical <a href="http://en.wikipedia.org/wiki/Lambda_calculus#Lambda_calculus_and_programming_languages">Lambda notation</a>.</p><p>Example</p><pre class="active4d code-block">
  <span class="Storage">function</span>(x) x <span class="Operator">*</span> x
</pre><pre class="active4d code-block">
  elems.some(<span class="Storage">function</span>(elem) elem.<span class="NamedConstant">type</span> <span class="Operator">==</span> <span class="String"><span class="String">&quot;</span>text<span class="String">&quot;</span></span>);
</pre>
<h2>Classes <a class='hh' name='Classes'>⁋</a></h2><p>Classes in <a href="JooScript.html">JooScript</a> is a handy alternative to prototyping. Although you can use <code>prototype</code> property as well.</p><p>Each class method has a special argument <code>self</code> which is a class instance.</p><p>General syntax</p><pre class="active4d code-block">
  [namespace] <span class="Storage">class</span> [ClassId] [as role] (ancestorA, ancestorB [, ...]) {
    [namespace] member [<span class="Operator">=</span> expression [, ...]]
    [constructor]
  }
</pre><p>Constructor syntax</p><pre class="active4d code-block">
  <span class="Storage">function</span> <span class="FunctionName">new</span> (<span class="FunctionArgument">arg1, arg2, arg3 [, ... rest]</span>) {
    [namespace] member [<span class="Operator">=</span> expression [, ...]]
    [<span class="Keyword">return</span> [expression]]
  }
</pre><p>Class features.</p><p>1. Inheritance:</p><pre class="active4d code-block">
  <span class="Storage">class</span> Mom as Cat () {
    tail <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>fuzzy<span class="String">&quot;</span></span>
    stripes <span class="Operator">=</span> <span class="Number">33</span>
    character <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>calm<span class="String">&quot;</span></span>
  }
 
  <span class="Storage">class</span> Caty as Cat (Mom) {
    stripes <span class="Operator">=</span> ancestors.Mom.stripes <span class="Operator">+</span> <span class="Number">27</span>
    character <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>playful<span class="String">&quot;</span></span>
  }
 
  let caty Cat <span class="Operator">=</span> Caty()
 
  <span class="Operator">with</span>(caty) trace(tail, stripes, character)
  <span class="LineComment"><span class="LineComment">//</span> will output 'fuzzy 60 playful'</span>
</pre><p>2. Using <code>self</code> and <code>parent</code>:</p><pre class="active4d code-block">
  role Attr {
    name <span class="LibraryClassType">String</span>,
    value <span class="LibraryClassType">String</span>
  }
 
  role Node {
    id Attr,
    ns Namespace
  }
 
  <span class="Storage">class</span> DocAttr as Attr () {
    <span class="Storage">function</span> <span class="FunctionName">new</span> (<span class="FunctionArgument">name, value</span>) {
      self.<span class="NamedConstant">id</span> <span class="Operator">=</span> id
      self.<span class="NamedConstant">value</span> <span class="Operator">=</span> value
      self.ns <span class="Operator">=</span> self.<span class="NamedConstant">parent</span>.ns
    }
  }
 
  <span class="Storage">class</span> Doc as Node () {
    <span class="Storage">function</span> <span class="FunctionName">new</span> (<span class="FunctionArgument">id, ns</span>) {
      self.ns <span class="Operator">=</span> Namespace(ns)
      self.<span class="NamedConstant">id</span> <span class="Operator">=</span> DocAttr(<span class="String"><span class="String">&quot;</span>id<span class="String">&quot;</span></span>, id)
    }
  }
  
  let rep <span class="Operator">=</span> Doc(<span class="String"><span class="String">&quot;</span>report<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>http://corp.com/ns/v10<span class="String">&quot;</span></span>)
</pre><p>3. Nesting classes:</p><pre class="active4d code-block">
  <span class="Storage">class</span> Duck () {
    <span class="Storage">class</span> Egg () {
      <span class="Storage">class</span> Donald () {}
    }
  }
</pre><p>4. Using <code>instanceof</code>:</p><pre class="active4d code-block">
  let donald <span class="Operator">=</span> Duck();
  <span class="Keyword">if</span>( donald <span class="Operator">instanceof</span> Duck )
    trace(<span class="String"><span class="String">'</span>Donald is Duck!<span class="String">'</span></span>)
</pre>
<h3>Operator <code>new</code> <a class='hh' name='Operator_<code>new</code>'>⁋</a></h3><p>For creating new class instances you have to call the class as function: <code>let duck = Duck()</code>, instead of using keyword <code>new</code>. Operator <code>new</code> is totally deprecated in <a href="JooScript.html">JooScript</a> due to syntax duplication in JavaScript: function may play double role, as method and as object constructor. In JooScript, function is just method. To construct new object, use classes.</p><p>But for backward compatibility, you can use static method <code>new()</code> for function as well as for class:</p><pre class="active4d code-block">
  <span class="Storage">function</span> <span class="FunctionName">OldSchoolClass</span>(<span class="FunctionArgument">a</span>){
    <span class="Variable">this</span>.a <span class="Operator">=</span> a
  }
  
  <span class="LibraryClassType">OldSchoolClass</span>.<span class="NamedConstant">prototype</span>.<span class="FunctionName">b</span> = <span class="Number">2</span>
 
  let old <span class="Operator">=</span> OldSchoolClass.<span class="Operator">new</span>(<span class="Number">1</span>)
  trace(old.a, old.b) <span class="LineComment"><span class="LineComment">//</span> will output 1 2</span>
</pre>
<h3><code>self</code> vs. <code>this</code> <a class='hh' name='<code>self</code>_vs._<code>this</code>'>⁋</a></h3><p>For some reasons, argument for class instance have a different name: <code>self</code> and <code>self</code> is not equivalent to <code>this</code>. Look at example:</p><pre class="active4d code-block">
  <span class="Storage">class</span> Test () {
    name <span class="Operator">=</span> <span class="String"><span class="String">&quot;</span>Test<span class="String">&quot;</span></span>
    <span class="Storage">function</span> <span class="FunctionName">testThis</span> () <span class="Variable">this</span>.<span class="NamedConstant">name</span>
    <span class="Storage">function</span> <span class="FunctionName">testSelf</span> () self.<span class="NamedConstant">name</span>
  }
 
  <span class="LibraryClassType">String</span>.<span class="NamedConstant">prototype</span>.<span class="FunctionName">name</span> = <span class="String"><span class="String">&quot;</span>String<span class="String">&quot;</span></span>
  <span class="LibraryClassType">String</span>.<span class="NamedConstant">prototype</span>.<span class="FunctionName">testThis</span> = Test().testThis
  <span class="LibraryClassType">String</span>.<span class="NamedConstant">prototype</span>.<span class="FunctionName">testSelf</span> = Test().testSelf
  
  <span class="CommandMethod">print</span>(<span class="String"><span class="String">&quot;</span>abc<span class="String">&quot;</span></span>.testThis(), <span class="String"><span class="String">&quot;</span>abc<span class="String">&quot;</span></span>.testSelf()) <span class="LineComment"><span class="LineComment">//</span> =&gt; String, Test</span>
</pre>
<h2>Packages <a class='hh' name='Packages'>⁋</a></h2><p>Packages are just directories to store your classes and methods in, to keep you code organized instead of have hundreds of classes in the same folder. Or simply package is a group of classes.</p><p>General syntax:</p><pre class="active4d code-block">
  <span class="Keyword">package</span> [group] {
    <span class="Storage">public</span> <span class="Storage">class</span> ClassId () {
      [namespace] member [<span class="Operator">=</span> expression [, ...]]
      [constructor]
    }
    [local | let] variable [<span class="Operator">=</span> expression [, ...]]
  }
</pre><p>The only namespace allowed in package declaration is <code>local</code> (or <code>let</code>). It is also default namespace for new variables and methods. </p><p>Once you create package, the class registered in the scope with group namespace. </p><p>Packages and classes could be nested:</p><pre class="active4d code-block">
  <span class="Keyword">package</span> a { 
    <span class="Storage">public</span> <span class="Storage">class</span> A (){} 
      <span class="Keyword">package</span> b { 
        <span class="Storage">public</span> <span class="Storage">class</span> B (){ 
          <span class="Keyword">package</span> c { 
             <span class="Storage">public</span> <span class="Storage">class</span> C (){} } } } }
  
  <span class="CommandMethod">print</span>(A.<span class="NamedConstant">className</span>, B.<span class="NamedConstant">className</span>, C.<span class="NamedConstant">className</span>)       <span class="LineComment"><span class="LineComment">//</span> =&gt; A, B, C</span>
  <span class="CommandMethod">print</span>(a.A.<span class="NamedConstant">className</span>, b.B.<span class="NamedConstant">className</span>, c.C.<span class="NamedConstant">className</span>) <span class="LineComment"><span class="LineComment">//</span> =&gt; A, B, C</span>
</pre><p><strong>Note</strong>: In strict mode group of package must be equivalents to relative module path:</p><pre class="active4d code-block">
  <span class="LineComment"><span class="LineComment">//</span> file: {lib}/w3c/xpath/XPath.joo</span>
  <span class="Keyword">package</span> w3c.xpath {
    <span class="Storage">public</span> <span class="Storage">class</span> XPath (){
      <span class="LineComment"><span class="LineComment">//</span> ... it's ok</span>
    }
  }
</pre>
<h2>Import directive <a class='hh' name='Import_directive'>⁋</a></h2><p><code>import</code> directive makes you happy when codebase is growing like a bread on the yeast.</p><p>General syntaxes</p><pre class="active4d code-block">
  <span class="Keyword">import</span> [namespace::][groupName.]moduleName [as localAlias]
</pre><pre class="active4d code-block">
  from [namespace::]groupName <span class="Keyword">import</span> moduleName [as localAlias]
</pre><pre class="active4d code-block">
  <span class="Keyword">import</span> moduleName [as localAlias] from [namespace::]groupName
</pre><p>Examples:</p><pre class="active4d code-block">
  <span class="Keyword">import</span> pack.battery.Power;
  <span class="Keyword">import</span> Controls from pack.battery;
  from engine <span class="Keyword">import</span> Gears as G, Valve as V, Transmission as T;
</pre><p>Import from external resource:</p><pre class="active4d code-block">
  let repo <span class="Operator">=</span> Namespace(<span class="String"><span class="String">&quot;</span>http://repo/proj/src/lib<span class="String">&quot;</span></span>)
  <span class="Keyword">import</span> repo::IFace
</pre>
<h2>Event model syntax <a class='hh' name='Event_model_syntax'>⁋</a></h2><p><!-- <a href="JooScript.html">JooScript</a> is the first formal language (as far as I know), has introduced discrete event syntax. I don't know why, but this natural form of human representation of changes in the real world was not widely reflected in programming languages before. --></p><p><a href="JooScript.html">JooScript</a> is using proprietary syntax for catching-dispatching event.</p><p>In addition, when using new event model syntax, you will forget about difference between interpretation of standards in browsers.</p><p>General syntaxes</p><pre class="active4d code-block">
  [use capture <span class="Operator">=</span> <span class="BuiltInConstant">true</span>|<span class="BuiltInConstant">false</span>]
  when eventType [[<span class="Operator">in</span>|on|over] target] <span class="Keyword">do</span> eventHandler
  once eventType [[<span class="Operator">in</span>|on|over] target] <span class="Keyword">do</span> eventHandler
</pre><pre class="active4d code-block">
  dispatch eventType [to target] : <span class="Storage">boolean</span>
</pre><pre class="active4d code-block">
  remove eventType [from target]
</pre><p><!-- Where <code>eventHandler</code> can be either block statement <code>{}</code> or a function. Sample: --></p><p>Where <code>eventHandler</code> is a function.</p><p>When <code>target</code> is omitted, assumed event is global.</p><p>Example:</p><pre class="active4d code-block">
  <span class="Storage">function</span> <span class="FunctionName">showAlert</span>(<span class="FunctionArgument">msg</span>) 
    <span class="CommandMethod">alert</span>(msg.<span class="NamedConstant">type</span> <span class="Operator">+</span> <span class="String"><span class="String">&quot;</span>, <span class="String">&quot;</span></span> <span class="Operator">+</span> msg.<span class="NamedConstant">target</span>.<span class="NamedConstant">id</span>);
  
  when <span class="String"><span class="String">&quot;</span>mouseclick<span class="String">&quot;</span></span> on pageElement <span class="Keyword">do</span> showAlert;
</pre>
<h2>E4X: XML-juggling <a class='hh' name='E4X:_XML-juggling'>⁋</a></h2><p>XML fragments and lists are first-class citizens in JooScript:</p><pre class="active4d code-block">
  let x <span class="Operator">=</span> <span class="Operator">&lt;</span>people <span class="Storage">class</span><span class="Operator">=</span><span class="String"><span class="String">&quot;</span>example<span class="String">&quot;</span></span><span class="Operator">&gt;</span>
      <span class="Operator">&lt;</span>person id<span class="Operator">=</span><span class="String"><span class="String">&quot;</span>1<span class="String">&quot;</span></span><span class="Operator">&gt;</span><span class="Operator">&lt;</span>name<span class="Operator">&gt;</span>Sam<span class="Operator">&lt;</span>/name<span class="Operator">&gt;</span><span class="Operator">&lt;</span>/person<span class="Operator">&gt;</span>
      <span class="Operator">&lt;</span>person id<span class="Operator">=</span><span class="String"><span class="String">&quot;</span>2<span class="String">&quot;</span></span><span class="Operator">&gt;</span><span class="Operator">&lt;</span>name<span class="Operator">&gt;</span>Elizabeth<span class="Operator">&lt;</span>/name<span class="Operator">&gt;</span><span class="Operator">&lt;</span>/person<span class="Operator">&gt;</span>
    <span class="Operator">&lt;</span>/people<span class="Operator">&gt;</span>; <span class="LineComment"><span class="LineComment">//</span></span>
 
  let staff <span class="Operator">=</span> [];
  <span class="Keyword">for</span> each (let elm <span class="Operator">in</span> x.person) {
      staff.<span class="CommandMethod">push</span>(elm.<span class="NamedConstant">name</span> <span class="Operator">+</span> <span class="String"><span class="String">&quot;</span>, <span class="String">&quot;</span></span> <span class="Operator">+</span> elm.@id)
  }
 
  <span class="CommandMethod">print</span>(staff.<span class="CommandMethod">join</span>(<span class="String"><span class="String">&quot;</span>; <span class="String">&quot;</span></span>)) <span class="LineComment"><span class="LineComment">//</span> will output Sam, 1; Elizabeth, 2</span>
</pre><p><a href="EcmaE4X.html">In-depth introduction for E4X</a>.</p><p><a href="JooScript.html">JooScript</a> has it's own E4X implementation. It means E4X should supported on all platforms. <h2>Magic keywords <a class='hh' name='Magic_keywords'>⁋</a></h2></p><p>Magic keywords is an internal keywords, they helps in code debugging.</p><ul><li><strong><code>__PATH__</code></strong>: module path</li><li><strong><code>__FILE__</code></strong>: module file name</li><li><strong><code>__FUNC__</code></strong>: function name</li><li><strong><code>__CLASS__</code></strong>: class name</li><li><strong><code>__GROUP__</code></strong>: group name</li><li><strong><code>__LINE__</code></strong>: line of source</li></ul>
 <i>The content of this page is licensed under the <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License</a>. (Ɔ) 2009-2010 <a href="mailto:bu(z*2)ilo-at-gmail-dot-com.html">Evgeny Burzak</a>.</i>
        <hr/>
        Updated: Sun Jan 30 22:11:42 +0300 2011
    </body>
</html>
